package com.example.workbench.controller;

import com.example.workbench.common.api.CommonResult;
import com.example.workbench.common.command.AddCardCommand;
import com.example.workbench.common.command.DelByIdCommand;
import com.example.workbench.common.command.UpdateCardCommand;
import com.example.workbench.common.command.UpdateSameLineCommand;
import com.example.workbench.entity.DbTaskCard;
import com.example.workbench.service.IDbTaskCardService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-08-03
 */
@RestController
@RequestMapping("/card")
public class DbTaskCardController {
    @Autowired
    private IDbTaskCardService cardService;

    @PostMapping("/query")
    private CommonResult<List<DbTaskCard>> queryCard(@RequestBody DelByIdCommand command) {
        if (StringUtils.isEmpty(String.valueOf(command.getId()))) {
            return CommonResult.failed("参数不能为空");
        }
        List<DbTaskCard> list = cardService.queryCard(command);
        return CommonResult.success(list);
    }
    @PostMapping("/add")
    private CommonResult<String> addCard(@RequestBody AddCardCommand command) {
        if (StringUtils.isEmpty(String.valueOf(command.getCardName()))) {
            return CommonResult.failed("参数不能为空");
        }
        cardService.addCard(command);
        return CommonResult.success("");
    }
    @PostMapping("/update")
    private CommonResult<String> updateCard(@RequestBody UpdateCardCommand command) {
        if (StringUtils.isEmpty(String.valueOf(command.getCardName()))) {
            return CommonResult.failed("参数不能为空");
        }
        cardService.updateCard(command);
        return CommonResult.success("");
    }

    @PostMapping("/del")
    private CommonResult deleteCard(@RequestBody DelByIdCommand command) {
        if (StringUtils.isEmpty(String.valueOf(command.getId()))) {
            return CommonResult.failed("参数不能为空");
        }
        return cardService.deleteCard(command);
    }

    @PostMapping("/updateSameLineSort")
    private CommonResult updateSameLineSort(@RequestBody UpdateSameLineCommand command) {
        cardService.updateSameLineSort(command.getList());
        return CommonResult.success("");
    }
}
